#include "uart.h"
#include "common.h"
#include "Ymodem.h"


uint32_t JumpAddress;
void (*Jump_To_Application)(void);

void jump_Application(void)
{
	if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
	{
		JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
		Jump_To_Application = (void(*)()) JumpAddress;
		__set_MSP(*(__IO uint32_t*) ApplicationAddress);
		Jump_To_Application();
	}
}
int main(void)
{
	uint8_t buf[1024];
	uint8_t key;
	int32_t status;
	FLASH_Unlock();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	Uart4_Init(115200);
	
	
	while(1)
	{
		SendStr("Update program press 0x41,Press 0x42 for normal startup\r\n");
		Receive_Byte(&key,0xFFFFFFFF);
		if(key == 0x41)
		{
			status = Ymodem_Receive(buf);
			if(status>0)
			{
				SendStr("Successful Write\r\n");
			}
			else
				SendStr("Update failure\r\n");
		}
		else
		{
			SendStr("jump Application\r\n");
			jump_Application();
		}
	}

}
